package org.br.erp.rfq.xbj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import okhttp3.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.SecurityUtils;
import org.br.erp.base.entity.Scmpartner;
import org.br.erp.base.service.IScmpartnerService;
import org.br.erp.base.utils.ERPUtils;
import org.br.erp.base.utils.HttpUtils;
import org.br.erp.rfq.base.entity.Scmuserregionlink;
import org.br.erp.rfq.base.service.IScmuserregionlinkService;
import org.br.erp.rfq.quo.entity.Scmquotation;
import org.br.erp.rfq.quo.mapper.ScmquotationMapper;
import org.br.erp.rfq.xbj.entity.Scmxjd;
import org.br.erp.rfq.xbj.entity.Scmxjditem;
import org.br.erp.rfq.xbj.mapper.ScmxjditemMapper;
import org.br.erp.rfq.xbj.mapper.ScmxjdMapper;
import org.br.erp.rfq.xbj.service.IScmxjdService;
import org.br.erp.rfq.xbj.vo.ScmVendorUnQuoList;
import org.br.erp.rfq.xbj.vo.ScmxjdVo;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.base.mapper.BaseCommonMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 询价单
 * @Author: jeecg-boot
 * @Date:   2023-09-16
 * @Version: V1.0
 */
@Service
public class ScmxjdServiceImpl extends ServiceImpl<ScmxjdMapper, Scmxjd> implements IScmxjdService {

	@Autowired
	private ScmxjdMapper scmxjdMapper;
	@Autowired
	private ScmxjditemMapper scmxjditemMapper;


	@Autowired
	private IScmpartnerService scmpartnerService;

	@Autowired
	private BaseCommonMapper baseCommonMapper;


	@Autowired
	private ScmquotationMapper scmquotationMapper;

	@Autowired
	private IScmuserregionlinkService scmuserregionlinkService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Scmxjd scmxjd, List<Scmxjditem> scmxjditemList) {
		//scmxjd.setBillcode("XJD2023090001");
		String yyyyMM = ERPUtils.getDateString(new Date(), "yyyyMMdd");
		//查找询价单号
		QueryWrapper<Scmxjd> queryWrapper=new QueryWrapper<>();
		queryWrapper.likeRight("billcode","XJD"+yyyyMM );
		queryWrapper.orderByDesc("billcode");

		List<Scmxjd> scmxjds = scmxjdMapper.selectList(queryWrapper);
		if(scmxjds.size() == 0){
			scmxjd.setBillcode("XJD"+yyyyMM+"0001");
		}else{
			Scmxjd maxXjd = scmxjds.get(0);
			String billcode = maxXjd.getBillcode();
			String substring = billcode.substring("XJD20230923".length());
			int i = Integer.parseInt(substring);
			int j =  i+1;
			String newBillCode = String.format("%04d",j);
			scmxjd.setBillcode("XJD"+yyyyMM+newBillCode);
		}
		scmxjd.setAuditstatus("6");
		scmxjd.setStatus("1");
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		scmxjd.setCreatebyname(sysUser.getUsername());
		scmxjdMapper.insert(scmxjd);
		if(scmxjditemList!=null && scmxjditemList.size()>0) {
			BigDecimal sumCount = BigDecimal.ZERO;
			for(Scmxjditem entity:scmxjditemList) {
				//外键设置
				entity.setParentid(scmxjd.getId());
				entity.setQuoflag("0");
				entity.setPoflag("0");
				String count = entity.getCount();
				sumCount = sumCount.add(ERPUtils.getBigDecimal(count));
				scmxjditemMapper.insert(entity);
			}
			scmxjd.setPartcount(sumCount);
			scmxjdMapper.updateById(scmxjd);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Scmxjd scmxjd,List<Scmxjditem> scmxjditemList) {
		if(scmxjd.getAuditstatus()!=null && (scmxjd.getAuditstatus().equalsIgnoreCase("2") || scmxjd.getAuditstatus().equalsIgnoreCase("4"))){
			scmxjd.setAuditstatus("6");
		}

		if(scmxjd.getAuditstatus()!=null && !scmxjd.getAuditstatus().equalsIgnoreCase("6")){
			throw new RuntimeException("只允许未提交审核的询价单编辑修改");
		}
		scmxjdMapper.updateById(scmxjd);
		
		//1.先删除子表数据
		scmxjditemMapper.deleteByMainId(scmxjd.getId());
		
		//2.子表数据重新插入
		if(scmxjditemList!=null && scmxjditemList.size()>0) {
			for(Scmxjditem entity:scmxjditemList) {
				//外键设置
				entity.setParentid(scmxjd.getId());
				if(entity.getQuoflag()==null){
					entity.setQuoflag("0");
				}
				scmxjditemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
//		scmxjditemMapper.deleteByMainId(id);
//		scmxjdMapper.deleteById(id);
		Scmxjd scmxjd = scmxjdMapper.selectById(id);
		scmxjd.setStatus("0");//更新成待删除
		scmxjdMapper.updateById(scmxjd);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
//			scmxjditemMapper.deleteByMainId(id.toString());
//			scmxjdMapper.deleteById(id);
			Scmxjd scmxjd = scmxjdMapper.selectById(id);
			scmxjd.setStatus("0");//更新成待删除
			scmxjdMapper.updateById(scmxjd);
		}
	}

	@Override
	public IPage<Scmxjd> getXjdList(IPage<Scmxjd> page, QueryWrapper<Scmxjd> queryWrapper) {

		//判断当前登录人
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String id = sysUser.getId();

		String provinceflag = null;
		String regionflag = null;
		String regionid = null;

		List<Map> sys_user = baseCommonMapper.basequery("provinceflag,regionid", "sys_user", "id ='" + id + "'");

		if(sys_user!=null && sys_user.size()>0){
			Map map = sys_user.get(0);
			if(map.containsKey("provinceflag")){
				provinceflag = map.get("provinceflag").toString();
			}
			if(map.containsKey("regionid")){
				regionid=map.get("regionid").toString();
			}
		}
		//如果是省公司的人，则看到全部的数据
		if(provinceflag!=null && provinceflag.equalsIgnoreCase("0")){
			//省公司
//			queryWrapper.likeRight("cityid",regionid.substring(0,2));

			QueryWrapper<Scmuserregionlink> scmuserregionlinkQueryWrapper=new QueryWrapper<>();
			scmuserregionlinkQueryWrapper.eq("userid",sysUser.getId());
			List<Scmuserregionlink> scmuserregionlinks = scmuserregionlinkService.list(scmuserregionlinkQueryWrapper);

			if(scmuserregionlinks.size() == 0){
				queryWrapper.eq("1","2");
			}

			boolean isflag= false;

			for (Scmuserregionlink scmuserregionlink : scmuserregionlinks) {
				String curRegionId = scmuserregionlink.getRegionid();
				if(curRegionId == null){
					isflag=true;
					break;
				}
			}

			if(isflag){
				//查询当前全部省市下的数据
				queryWrapper.likeRight("cityid",regionid.substring(0,2));
			}else{
				if(scmuserregionlinks.size()>0){
					String sql=" (";
					for (Scmuserregionlink scmuserregionlink : scmuserregionlinks) {
						String curRegionId = scmuserregionlink.getRegionid();
//					queryWrapper.or(item->item.or().likeRight("cityid",curRegionId.substring(0,4)));
						sql += " head.cityid like '"+curRegionId.substring(0,4)+"%' or ";
					}

					sql = sql.substring(0,sql.length()-3)+")";

					queryWrapper.exists("select 1 from scmxjd head where    head.id = scmxjd.id and "+ sql);
				}else{
					queryWrapper.eq("1","2");
				}

			}
//			queryWrapper.eq("auditstatus","1");
			queryWrapper.and(item->item.eq("auditstatus","1").or(q->q.eq("auditstatus","3")).or(a->a.eq("auditstatus","5")));


		}
		else if(provinceflag!=null && provinceflag.equalsIgnoreCase("1")){
			//地区公司
			queryWrapper.likeRight("cityid",regionid.substring(0,4));
//			queryWrapper.eq("auditstatus","0");
		}else if(provinceflag!=null && provinceflag.equalsIgnoreCase("2")){
			//只能查看本人
			queryWrapper.eq("create_by",sysUser.getUsername());
		}else {
			queryWrapper.eq("create_by","1==2");
		}
		return scmxjdMapper.selectPage(page,queryWrapper);
	}

	@Override
	@Transactional
	public void updateXJDStatus(List<Map> ids) {
		for (Map idObject : ids) {
			String id = idObject.get("id").toString();
			Scmxjd scmxjd = scmxjdMapper.selectById(id);


			//判断当前状态
			String auditstaus = idObject.get("auditstatus").toString();

			if(scmxjd.getAuditstatus()!=null && scmxjd.getAuditstatus().equalsIgnoreCase(auditstaus)){
				continue;
			}

			if(scmxjd.getAuditstatus()!=null && (scmxjd.getAuditstatus().equalsIgnoreCase("2") || scmxjd.getAuditstatus().equalsIgnoreCase("4"))&& auditstaus.equalsIgnoreCase("0")){
				throw new RuntimeException("当前单据被驳回，需修改编辑才能进行提交");
			}

			if(scmxjd.getAuditstatus()!=null){
				String orgstatus = scmxjd.getAuditstatus();
				if(orgstatus.equalsIgnoreCase("1") && auditstaus.equalsIgnoreCase("2") ){
					throw new RuntimeException("已审批通过，不允许审批不通过");
				}
				if(orgstatus.equalsIgnoreCase("3") && auditstaus.equalsIgnoreCase("4")){
					throw new RuntimeException("已审批通过，不允许审批不通过");
				}
				if(orgstatus.equalsIgnoreCase("5") && (auditstaus.equalsIgnoreCase("4")|| auditstaus.equalsIgnoreCase("2"))){
					throw new RuntimeException("已审批通过，不允许审批不通过");
				}

				if(orgstatus.equalsIgnoreCase("5") && auditstaus.equalsIgnoreCase("3")){
					throw new RuntimeException("已审批通过，不允许再次审批");
				}
			}
			scmxjd.setAuditstatus(auditstaus);

			if(scmxjd.getAuditstatus().equalsIgnoreCase("1")){
				scmxjd.setStatus("2");
			}else if(scmxjd.getAuditstatus().equalsIgnoreCase("2")){
				scmxjd.setStatus("3");
			}else if(scmxjd.getAuditstatus().equalsIgnoreCase("3")){
				scmxjd.setStatus("4");
			}else if(scmxjd.getAuditstatus().equalsIgnoreCase("4")){
				scmxjd.setStatus("5");
			}else if(scmxjd.getAuditstatus().equalsIgnoreCase("5")){
				scmxjd.setStatus("6");
			}else if(scmxjd.getAuditstatus().equalsIgnoreCase("6")){
				scmxjd.setStatus("7");
			}
			if(idObject.containsKey("rejectreason")){
				String rejectreason = idObject.get("rejectreason").toString();
				if(auditstaus.equalsIgnoreCase("2")){
					//地区驳回意见
					String regionrejectreason = scmxjd.getRegionrejectreason();
					if(regionrejectreason==null){
						regionrejectreason ="";
					}
					scmxjd.setRegionrejectreason(regionrejectreason+rejectreason);
				}else if(auditstaus.equalsIgnoreCase("4")){
					String provicerejectreason = scmxjd.getProvicerejectreason();
					if(provicerejectreason==null){
						provicerejectreason="";
					}
					scmxjd.setProvicerejectreason(provicerejectreason+rejectreason);
				}
			}
			scmxjdMapper.updateById(scmxjd);
		}
	}

	@Override
	@Transactional
	public void updatevendorid(List<Map> ids) {
		for (Map idObject : ids) {
			if(!idObject.containsKey("id")){
				throw new RuntimeException("当前询价单有问题，请查看");
			}

			if(!idObject.containsKey("vendorid")){
				throw new RuntimeException("未选择供应商！");
			}

			String id = idObject.get("id").toString();

			String vendorid = idObject.get("vendorid").toString();

			Scmxjd scmxjd = scmxjdMapper.selectById(id);

			scmxjd.setReqvendorid(vendorid);

			String[] split = vendorid.split(",");
			List<Scmpartner> scmpartners = scmpartnerService.listByIds(Arrays.asList(split));
			StringBuilder p=new StringBuilder();
			for (Scmpartner scmpartner : scmpartners) {
				String partnername = scmpartner.getPartnername();
				p.append(partnername).append(",");
			}
			String substring = p.substring(0, p.length() - 1);
			scmxjd.setAuditstatus("5");
			scmxjd.setStatus("6");
			scmxjd.setReqvendorname(substring);
			scmxjdMapper.updateById(scmxjd);
		}
	}

	@Override
	public List<ScmVendorUnQuoList> selectVendorUnQuoList(IPage<ScmVendorUnQuoList> page,QueryWrapper<ScmVendorUnQuoList> queryWrapper) {

		 List<ScmVendorUnQuoList> res =  new ArrayList<>();
//		QueryWrapper<ScmVendorUnQuoList> queryWrapper=new QueryWrapper<>();
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		String selecttablevalue = baseCommonMapper.selecttablevalue("sys_user", "innervendorid", "id", sysUser.getId());

		if(selecttablevalue!=null && !selecttablevalue.equalsIgnoreCase("")){
			queryWrapper.like("head.reqvendorid",selecttablevalue);
//			queryWrapper.eq("item.quoflag","0");
			queryWrapper.eq("head.auditstatus","5");
			queryWrapper.notExists("select 1 from scmquotation where fromitemid = item.id and vendorid ='"+selecttablevalue+"'");
			queryWrapper.orderByDesc("head.create_time");
			List<ScmVendorUnQuoList> vendorUnQuoList = scmxjdMapper.getVendorUnQuoList(page, queryWrapper);
			for (ScmVendorUnQuoList scmVendorUnQuoList : vendorUnQuoList) {
				scmVendorUnQuoList.setVendorid(selecttablevalue);
			}
			return vendorUnQuoList;
		}else{
			return res;
		}

	}

	//获取行驶证信息
	@Override
	public JSONObject getXSZInfo(String jsonObject) {
		try {
			String host = "https://car3.market.alicloudapi.com";
			String path = "/ocrDrivingLicense";
			String method = "POST";
			String appcode = "8e0b015a63904ce7b9716694a2cb1823";
			Map<String, String> headers = new HashMap<String, String>();
			//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
			headers.put("Authorization", "APPCODE " + appcode);
			//根据API的要求，定义相对应的Content-Type
			headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			Map<String, String> querys = new HashMap<String, String>();
			Map<String, String> bodys = new HashMap<String, String>();
			bodys.put("img_base64", jsonObject);
			bodys.put("img_url", "");
			bodys.put("side", "1");

			/**
			 * 重要提示如下:
			 * HttpUtils请从
			 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
			 * 下载
			 *
			 * 相应的依赖请参照
			 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
			 */
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//			System.out.println(response.toString());
//			EntityUtils.toString(response.getEntity());
			JSONObject parse = (JSONObject) JSON.parse(EntityUtils.toString(response.getEntity()));
			if(parse.containsKey("showapi_res_body")){
				return (JSONObject) parse.get("showapi_res_body");
			}else{
				return null;
			}
		}catch (Exception e){
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<ScmxjdVo> getVendorQuoList(IPage<Scmxjd> page, String bjstatus) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		String selecttablevalue = baseCommonMapper.selecttablevalue("sys_user", "innervendorid", "id", sysUser.getId());

		if(selecttablevalue == null || selecttablevalue.equalsIgnoreCase("")){
			return null;
		}

		QueryWrapper<Scmxjd> scmxjdQueryWrapper=new QueryWrapper<>();
		scmxjdQueryWrapper.like("reqvendorid",selecttablevalue);

		if(bjstatus.equalsIgnoreCase("0")){
			//未报价
			scmxjdQueryWrapper.notExists("select 1 from scmquotation   where fromid = scmxjd.id and vendorid ='"+selecttablevalue+"'");
		}else if(bjstatus.equalsIgnoreCase("1")){
			//已报价
			scmxjdQueryWrapper.exists("select 1 from scmquotation where fromid = scmxjd.id and vendorid = '"+selecttablevalue+"'");
		}else if(bjstatus.equalsIgnoreCase("2")){
			//已下订单
			scmxjdQueryWrapper.exists("select 1 from scmpoitem where xjdid = scmxjd.id and exists(select 1 from scmpo where scmpo.id = scmpoitem.parentid and vendorid ='"+selecttablevalue+"')");
		}else if(bjstatus.equalsIgnoreCase("3") || bjstatus.equalsIgnoreCase("")){
			//显示全部
		}

		scmxjdQueryWrapper.orderByDesc("create_time");
		IPage<Scmxjd> scmxjdIPage = scmxjdMapper.selectPage(page, scmxjdQueryWrapper);
		List<Scmxjd> records = scmxjdIPage.getRecords();
		List<ScmxjdVo> res=new ArrayList<>();
		for (Scmxjd record : records) {
			ScmxjdVo scmxjdVo=new ScmxjdVo();
			BeanUtils.copyProperties(record,scmxjdVo);
			QueryWrapper<Scmquotation> scmquotationQueryWrapper=new QueryWrapper<>();
			scmquotationQueryWrapper.eq("fromid",record.getId());
			scmquotationQueryWrapper.eq("vendorid",selecttablevalue);
			List<Scmquotation> scmquotations = scmquotationMapper.selectList(scmquotationQueryWrapper);
			List<Map> id = baseCommonMapper.basequery("head.id", "scmpo head\n" +
					"inner join scmpoitem item on item.parentid  = item.id", " head.vendorid ='" + selecttablevalue + "' and item.xjdid='" + scmxjdVo.getId() + "'");
			scmxjdVo.setQuostate("未报价");
			if(scmquotations.size()>0){
				scmxjdVo.setQuostate("已报价");
			}
			if(id.size()>0){
				scmxjdVo.setQuostate("已下订单");
			}
			scmxjdVo.setVendorid(selecttablevalue);
			res.add(scmxjdVo);
		}
		return res;

	}

	@Override
	public List<ScmxjdVo> getNotPOXjdAndQuoList(IPage<Scmxjd> page, String bjstatus,QueryWrapper<Scmxjd> scmxjdQueryWrapper) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String id = sysUser.getId();
		String provinceflag = null;
		String regionflag = null;
		String regionid = null;

		List<Map> sys_user = baseCommonMapper.basequery("provinceflag,regionid", "sys_user", "id ='" + id + "'");

		if(sys_user!=null && sys_user.size()>0){
			Map map = sys_user.get(0);
			if(map.containsKey("provinceflag")){
				provinceflag = map.get("provinceflag").toString();
			}
			if(map.containsKey("regionid")){
				regionid=map.get("regionid").toString();
			}
		}

		if(provinceflag!=null && provinceflag.equalsIgnoreCase("0")){
			//省公司
//			queryWrapper.likeRight("cityid",regionid.substring(0,2));

			QueryWrapper<Scmuserregionlink> scmuserregionlinkQueryWrapper=new QueryWrapper<>();
			scmuserregionlinkQueryWrapper.eq("userid",sysUser.getId());
			List<Scmuserregionlink> scmuserregionlinks = scmuserregionlinkService.list(scmuserregionlinkQueryWrapper);

			if(scmuserregionlinks.size() == 0){
				scmxjdQueryWrapper.eq("1","2");
			}

			boolean isflag= false;

			for (Scmuserregionlink scmuserregionlink : scmuserregionlinks) {
				String curRegionId = scmuserregionlink.getRegionid();
				if(curRegionId == null){
					isflag=true;
					break;
				}
			}

			if(isflag){
				//查询当前全部省市下的数据
				scmxjdQueryWrapper.likeRight("cityid",regionid.substring(0,2));
			}else{
        if (scmuserregionlinks.size() > 0) {
          String sql = " (";
          for (Scmuserregionlink scmuserregionlink : scmuserregionlinks) {
            String curRegionId = scmuserregionlink.getRegionid();
            //					queryWrapper.or(item->item.or().likeRight("cityid",curRegionId.substring(0,4)));
            sql += " head.cityid like '" + curRegionId.substring(0, 4) + "%' or ";
          }

          sql = sql.substring(0, sql.length() - 3) + ")";

          scmxjdQueryWrapper.exists(
              "select 1 from scmxjd head where    head.id = scmxjd.id and " + sql);
				}else{
			scmxjdQueryWrapper.eq("1","2");
		}
			}
//			queryWrapper.eq("auditstatus","1");
//			scmxjdQueryWrapper.and(item->item.eq("auditstatus","1").or(q->q.eq("auditstatus","3")).or(a->a.eq("auditstatus","5")));


		}

//		QueryWrapper<Scmxjd> scmxjdQueryWrapper=new QueryWrapper<>();
		if(bjstatus.equalsIgnoreCase("0")){
			//未报价
//			scmxjdQueryWrapper.notExists("select 1 from scmquotation   where fromid = scmxjd.id and vendorid =");
		}else if(bjstatus.equalsIgnoreCase("1")){
			//已报价
			scmxjdQueryWrapper.exists("select 1 from scmquotation where fromid = scmxjd.id");
			scmxjdQueryWrapper.notExists("select 1 from scmxjditem where parentid = scmxjd.id and scmxjditem.poflag='1'");

		}else if(bjstatus.equalsIgnoreCase("2")){
			//已下订单
//			scmxjdQueryWrapper.exists("select 1 from scmpoitem where xjdid = scmxjd.id and exists(select 1 from scmpo where scmpo.id = scmpoitem.parentid and vendorid ='"+selecttablevalue+"')");
		}else if(bjstatus.equalsIgnoreCase("3") || bjstatus.equalsIgnoreCase("")){
			//显示全部
		}


		scmxjdQueryWrapper.orderByDesc("create_time");
		IPage<Scmxjd> scmxjdIPage = scmxjdMapper.selectPage(page, scmxjdQueryWrapper);
		List<Scmxjd> records = scmxjdIPage.getRecords();
		List<ScmxjdVo> res=new ArrayList<>();
		for (Scmxjd record : records) {
		  ScmxjdVo scmxjdVo = new ScmxjdVo();
		  BeanUtils.copyProperties(record, scmxjdVo);
		  res.add(scmxjdVo);
		}

		return res;


	}

	@Override
	public JSONArray getReqVendorInfo(String xjdid) {
		Scmxjd scmxjd = scmxjdMapper.selectById(xjdid);
		if(scmxjd.getReqvendorid()==null || scmxjd.getReqvendorid().equalsIgnoreCase("")){
			throw new RuntimeException("当前询价单未指派供应商");
		}
		String reqvendorid = scmxjd.getReqvendorid();
		String[] vendors = reqvendorid.split(",");
		JSONArray res = new JSONArray();
		for (String vendor : vendors) {
			JSONObject vendorObj=new JSONObject();
			vendorObj.put("vendorid",vendor);
			Scmpartner byId = scmpartnerService.getById(vendor);
			vendorObj.put("vendorname",byId.getPartnername());
			res.add(vendorObj);
		}
		return res;
	}
}
